package com.zretc.swaggerConfig;

import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import sun.plugin.dom.core.Document;
import java.lang.reflect.Array;
import java.util.ArrayList;

@Configuration
@EnableSwagger2    //开启Swaqger2
public class SwaggerConfig {


    //groupName()->配置多个分组
    @Bean
    public Docket docket1(){

        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2(){

        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }


    //配置了swagger的Docket的bean实例
    //访问路径：http://localhost:8080/swagger-ui.html#/
    @Bean
    public Docket docket(Environment environment) {

        //设置要显示的开发者环境
        Profiles profiles= Profiles.of("dev","test");
        //获取项目的环境：
        boolean flag= environment.acceptsProfiles(profiles);
        System.out.println("==="+flag);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("C")
                .enable(flag)  //enable是否启动swagger，如果为false,则swagger不能再浏览器中访问
                .select()
                //RequestHandlerSelectors配置要扫描的接口
                //basePackage:指定要扫描的包
                //any():扫描全部
                //none:不扫描
                .apis(RequestHandlerSelectors.basePackage("com.chen.controller"))
                //withClassAnnotation()扫描类上的注解，参数是一个注解的反射对象
                //withMethodAnnotation()扫描方法上的注解
                //.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                //paths() 过滤什么路径
                //.paths(PathSelectors.ant("/chen/**"))
                .build();

    }


    //配置Swagger信息==apiInfo
    private ApiInfo apiInfo(){

        //作者信息
        Contact contact=new Contact("陈锦贤","url","129@qq.com");
        return new ApiInfo("test Swagger文档",
                "Api Documentation",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0" ,
                "url",
                new ArrayList());

    }

}